home *** CD-ROM | disk | FTP | other *** search
- Path: news-m01.ny.us.ibm.net!usenet
- From: bbogard@ibm.net
- Newsgroups: comp.lang.c++
- Subject: Re: Virtual Constructors (how to implement)?
- Date: 21 Feb 1996 20:56:53 GMT
- Message-ID: <4gg0ul$401m@news-s01.ny.us.ibm.net>
- References: <4g0lle$nmq@jobe.shell.portal.com>
- Reply-To: bbogard@ibm.net
- NNTP-Posting-Host: slip37-223-98.ibm.net
- X-Newsreader: IBM NewsReader/2 v1.2.5
-
- In <4g0lle$nmq@jobe.shell.portal.com>, clare@shell.portal.com (Clare Chu) writes:
- >
- >I have an abstract base class Packet. Inherited from Packet
- >are specific packets like LoginPacket, AuditPacket, etc.
- >
- >What I need is to read a socket (stream), and then construct
- >the packet of the right kind, depending on what it is.
- >
- >I read a reference to this technique in Scott Meyer's More Effective C++
- >but I haven't received my copy yet. (It is back-ordered at Computer
- >Literacy).
- >
- >I need someone to basically outline what I need to do to set
- >up such a function that reads from a stream, produces the appropriate
- >object and returns a pointer to Packet pointing to the object
- >which is of the appropriate derived class.
- >
- >At present, I'm trying to implement it as an array of pointers
- >to functions. Those functions will be called based on the
- >Packet type, and create the appropriate object. Of course I
- >haven't even thought about going out of scope, destructors,
- >or such. My coworker says to just use a big switch statement
- >and construct objects based on the type.
- >
- >Any ideas?
- >
- >Thanks in advance,
- >Clare
- >clare@shell.portal.com
- >
- >
- >
-
- You could create an Abstract Factory type of object that given a certain object number
- and input stream, would create the proper object. That way all the creation of the objects
- would be centralized.
-
-